package com.bookmarket.controller;

import com.bookmarket.entity.*;
import com.bookmarket.service.LJOrderListService;
import com.bookmarket.utils.IdGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/order")
public class LJOrderController {


    @Resource
    private LJOrderListService ljOrderListService;


    //查询地址表
    @RequestMapping("/chaOrder")
    public ModelAndView chaOrder(Custom custom){
        Custom c=ljOrderListService.selectBy(custom.getName());
        List<Address> list=ljOrderListService.selectByCustom(c);
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }


    /******************************************************************/


    //提交(向订单表插入地址表相关信息；向关系表插入购物表id)
    @RequestMapping("/insert")
    public ModelAndView insert(int ids ,String shopid,float zongjia){
        Boolean flag=false;
        //根据id查地址
        Address a=ljOrderListService.selectById(ids);
        //根据地址信息存订单
        Order o=new Order();
        final IdGenerator id = IdGenerator.INSTANCE;
        o.setOrder_list(id.nextId());
        o.setCreateTime(new Date());
        o.setRename(a.getUname());
        o.setPhone(a.getPhone());
        o.setAddress(a.getAddress());
        o.setOrder_state(0);
        o.setStatus(0);
        o.setZongjia(zongjia);
        //实例化一个用户对象
        Custom custom=new Custom();
        //根据地址id查对应地址中用户外键给用户对象
        custom.setId(ljOrderListService.chaAddressId(a.getId()));
        custom.setName(o.getRename());
        o.setCustom(custom);

        //插入数据
        boolean falg=ljOrderListService.insertByOrder(o);
        StringBuffer WIDbody = new StringBuffer();
        if(falg){
            System.out.println("订单插入成功");
            //得到要插入订单的商品集合(从里面得到书籍id)
            /**
             * 这里在数据库中使用了触发器，进行库存的减少
             *
             create trigger tg_insert_order
             after insert on tb_order_book
             for each ROW
             BEGIN
             update tb_book set stock=stock-new.amount where id=new.book_id
             END
             */
            List<Shop> list=new ArrayList<Shop>();
            String []sid=shopid.split(",");
            for(int i=0;i<sid.length;i++){
                Shop s=ljOrderListService.selectByShopId(Integer.parseInt(sid[i]));
                list.add(s);
                WIDbody.append(s.getBook_name()+",");
            }
            //给book设置id
            Book b=new Book();
            int sg=0;
            for(Shop ss:list){
                //根据list中id查看对应的book外键给book设置
                b.setId(ljOrderListService.chaId(ss.getId()));
                sg=ss.getBook_num();
                //关系表放入数据
                Order_Book ob=new Order_Book();
                ob.setOrder(o);
                ob.setBook(b);
                ob.setAmount(sg);
                //调用存关系表接口
                flag=ljOrderListService.insertOrderBook(ob);
            }
            //删除已下订单的书籍
            for(Shop s:list){
                ljOrderListService.deleteBookById(s.getId());
            }
        }
        ModelAndView mv=new ModelAndView();

        //修改付款状态为已付款
        ljOrderListService.modOrderState(o.getId());



        mv.addObject("WIDout_trade_no",o.getOrder_list());
        mv.addObject("WIDsubject",o.getRename()+"的书籍");
        mv.addObject("WIDtotal_amount",zongjia);
        mv.addObject("WIDbody",WIDbody.toString());
        mv.setViewName("alipay/LQ_order");
        return mv;
    }

    //删除订单
    @RequestMapping("/delOrderById")
    public boolean delOrderById(int id){
        boolean flag = ljOrderListService.delOrderByOrderList(id);
        return flag;
    }

    //删除优惠券
    @RequestMapping("/deleteDiscount")
    public boolean deleteDiscount(int id){
        boolean flag = ljOrderListService.deleteDiscount(id);
        return flag;
    }

    public LJOrderListService getLjOrderListService() {
        return ljOrderListService;
    }

    public void setLjOrderListService(LJOrderListService ljOrderListService) {
        this.ljOrderListService = ljOrderListService;
    }
}
